package com.guang.lock.condition.conditionsetdifference;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * @Description 测试waitset的顺序
 * @Author liguang
 * @Date 2022/10/20/21:06
 */
public class SyncronizedWaitSetTest {
    private final static Logger logger = LoggerFactory.getLogger(SyncronizedWaitSetTest.class);
    static Object object = new Object();

    static boolean flag = false;

    public static void main(String[] args) {

        new Thread(() -> {
            synchronized (object) {
                while (!flag) {
                    try {
                        logger.info("因为获取不到锁而进入到这里来");
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                logger.info("flag的值是：{}",flag);
            }
        }, "tom").start();


        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (object) {
            for (int i = 0; i < 5; i++) {
                new Thread(() -> {
                    synchronized (object) {
                        logger.info("coding coding coding");
                    }
                }, "t" + (i + 1)).start();
                try {
                    TimeUnit.MICROSECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            flag = true;
            object.notifyAll();
        }
    }
}
